package com.mairuide.bill.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by 敲代码的卡卡罗特
 * on 2018/12/21 16:39.
 */
public class CollectionsUtils {
    /**
     *
     * @author 刘中华
     * @date 2018/12/22 0:47
     * @param [list, rule]  需要排序的字段
     * @return void
     * @Description List<Map<String, Object> 类型的排序工具类
     */
    public static void getSortList(List<Map<String, Object>> list,String rule){

        // 按点击数倒序
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> arg0, Map<String, Object> arg1) {
                if(arg0.get(rule)==null||arg1.get(rule)==null){
                    return -1;
                }
                try{
                    Double hits0 =Double.valueOf(arg0.get(rule).toString());
                    Double hits1 =Double.valueOf(arg1.get(rule).toString());
                    return hits1.compareTo(hits0);
                }catch (Exception e){
                    e.printStackTrace();
                    return -1;
                }
            }
        });
    }

    public static List<Map<String, Object>> getLimitTen(List<Map<String, Object>> list){
        if(list!=null&&list.size()>10){
            List<Map<String, Object>> maps = list.subList(0, 10);
            return maps;
        }
        return list;
    }
    public static List<Map<String, Object>> getLimitTwenty(List<Map<String, Object>> list){
        if(list!=null&&list.size()>20){
            List<Map<String, Object>> maps = list.subList(0, 20);
            return maps;
        }
        return list;
    }
}
